﻿/////////////////////////////////////////////////////////////////////////
// MIT License
//
// Copyright (c) 2000-2011 OpenNETCF Consulting, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  
////////////////////////////////////////////////////////////////////////

#region Using directives

using System;
using System.Runtime.InteropServices;
using System.Text;

#endregion

namespace OpenNETCF.Windows.Forms
{
    internal class Native
    {

        internal struct POINT
        {
            public int x;
            public int y;
        } 

        internal struct MEASUREITEMSTRUCT
        {
            internal int CtlType;
            internal int CtlID;
            internal int itemID;
            internal int itemWidth;
            internal int itemHeight;
            internal int itemData;
        }

        internal struct DRAWITEMSTRUCT
        {
            internal int CtlType;
            internal int CtlID;
            internal int itemID;
            internal int itemAction;
            internal int itemState;
            internal IntPtr hwndItem;
            internal IntPtr hDC;
            internal RECT rcItem;
//            internal int left;
//            internal int top;
//            internal int right;
//            internal int bottom;
            internal int itemData;
        }

        internal struct RECT
        {
            internal int left;
            internal int top;
            internal int right;
            internal int bottom;
        }

        internal const int ODS_SELECTED = 0x0001;
        internal const int ODS_GRAYED = 0x0002;
        internal const int ODS_DISABLED = 0x0004;
        internal const int ODS_CHECKED = 0x0008;
        internal const int ODS_FOCUS = 0x0010;

        internal const int ODA_DRAWENTIRE = 0x0001;
        internal const int ODA_SELECT = 0x0002;
        internal const int ODA_FOCUS = 0x0004;

        internal const int TPM_TOPALIGN = 0x0000;
        internal const int TPM_VCENTERALIGN = 0x0010;
        internal const int TPM_BOTTOMALIGN = 0x0020;

        internal const int TPM_HORIZONTAL = 0x0000;     /* Horz alignment matters more */
        internal const int TPM_VERTICAL = 0x0040;     /* Vert alignment matters more */
        internal const int TPM_NONOTIFY = 0x0080;     /* Don't send any notification msgs */
        internal const int TPM_RETURNCMD = 0x0100;

        internal const int WM_DRAWITEM = 0x002B;
        internal const int WM_MEASUREITEM = 0x002C;

        internal const int TPM_LEFTALIGN = 0x0000;
        internal const int TPM_CENTERALIGN = 0x0004;
        internal const int TPM_RIGHTALIGN = 0x0008;

        internal const int MF_BYCOMMAND = 0x00000000;
        internal const int MF_BYPOSITION = 0x00000400;

        internal const int MF_SEPARATOR = 0x00000800;

        internal const int MF_ENABLED = 0x00000000;
        internal const int MF_GRAYED = 0x00000001;

        internal const int MF_UNCHECKED = 0x00000000;
        internal const int MF_CHECKED = 0x00000008;

        internal const int MF_STRING = 0x00000000;
        internal const int MF_OWNERDRAW = 0x00000100;

        internal const int MF_POPUP = 0x00000010;
        // @CESYSGEN IF GWES_COLUMN
        internal const int MF_MENUBARBREAK = 0x00000020;
        internal const int MF_MENUBREAK = 0x00000040;
        // @CESYSGEN ENDIF

        internal const int MF_UNHILITE = 0x00000000;
        internal const int MF_HILITE = 0x00000080;

        internal const int MF_END = 0x00000080;  /* Obsolete -- only used by old RES files */

        internal const int MFT_STRING = MF_STRING;
        internal const int MFT_MENUBARBREAK = MF_MENUBARBREAK;
        internal const int MFT_MENUBREAK = MF_MENUBREAK;
        internal const int MFT_OWNERDRAW = MF_OWNERDRAW;
        internal const int MFT_RADIOCHECK = 0x00000200;
        internal const int MFT_SEPARATOR = MF_SEPARATOR;

		[DllImport("coredll.dll", SetLastError = true)]
		public static extern IntPtr CreateCompatibleDC(IntPtr hdc); 

		[DllImport("coredll", SetLastError=true)]
		public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
 
		[DllImport("coredll", SetLastError=true)]
		public static extern IntPtr DeleteObject(IntPtr hObject);

		[DllImport("coredll.dll", SetLastError = true)]
		public static extern int GetTextExtentExPoint(
			IntPtr hdc,
			string lpszStr,
			int cchString,
			int nMaxExtent,
			out int lpnFit,
			int[] alpDx,
			ref SIZE lpSize);

		public struct SIZE
		{
			public int width;
			public int height;
		}

        [DllImport("coredll.dll")]
        internal static extern int DeleteMenu(
            IntPtr hMenu,
            int uPosition,
            int uFlags);

        [DllImport("coredll.dll")]
        internal static extern IntPtr CreatePopupMenu();

        [DllImport("coredll.dll")]
        internal static extern int InsertMenu(
            IntPtr hMenu,
            uint uPosition,
            uint uFlags,
            uint uIDNewItem,
            int lpNewItem);

        [DllImport("coredll.dll")]
        internal static extern int AppendMenu(
            IntPtr hMenu,
            int uFlags,
            int uIDNewItem,
            int lpNewItem);

        [DllImport("coredll.dll")]
        internal static extern int AppendMenu(
            IntPtr hMenu,
            int uFlags,
            int uIDNewItem,
            string lpNewItem);


//        [DllImport("coredll.dll")]
//        internal static extern int GetMenuItemInfo(
//            IntPtr hMenu,
//            int uItem,
//            int fByPosition,
//            ref MENUITEMINFO lpmii);

        [DllImport("coredll.dll")]
        internal static extern int TrackPopupMenuEx(
            IntPtr hmenu,
            int uFlags,
            int x,
            int y,
            IntPtr hwnd,
            IntPtr lptpm);

        [DllImport("coredll.dll")]
        internal static extern IntPtr GetSubMenu(
            IntPtr hMenu,
            int nPos);

        [DllImport("aygshell.dll")]
        internal static extern IntPtr SHFindMenuBar(
                        IntPtr hwnd);

        [DllImport("coredll.dll")]
        internal static extern int GetCursorPos(
        ref POINT lpPoint);

        [DllImport("coredll.dll")]
        internal static extern int EnableMenuItem(
            IntPtr hmenu,
            int uIDEnableItem,
            int uEnable);
    }
}
